//
//  SIXStatusPhotosView.m
//  weibo
//
//  Created by chenangel on 16/5/21.
//  Copyright © 2016年 chuhan. All rights reserved.
//

#import "SIXStatusPhotosView.h"
#import "SIXPhoto.h"
#import "SIXStatusPhotoView.h"

#define SIXStatusPhotoWH 70
#define SIXStatusPhotoMargin 10
#define SIXStatusPhotoMaxCol(count) ((count==4)?2:3)
@implementation SIXStatusPhotosView

- (id)initWithFrame:(CGRect)frame
{
    
    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}

- (void)setPhotos:(NSArray *)photos
{
    _photos = photos;
    
    NSUInteger photosCount = photos.count;
    
    // 创建足够数量的图片控件
    // 这里的self.subviews.count不要单独赋值给其他变量
    while (self.subviews.count < photosCount) {
        SIXStatusPhotoView *photoView = [[SIXStatusPhotoView alloc] init];
        [self addSubview:photoView];
    }
    
    // 遍历所有的图片控件，设置图片
    for (int i = 0; i<self.subviews.count; i++) {
        SIXStatusPhotoView *photoView = self.subviews[i];
        
        photoView.tag = i;
        //添加点按手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        [photoView addGestureRecognizer:tap];
        
        if (i < photosCount) { // 显示
            photoView.photo = photos[i];
            photoView.hidden = NO;
        } else { // 隐藏
            photoView.hidden = YES;
        }
    }
}
#pragma mark - 点击图片的时候调用


- (void)tap:(UITapGestureRecognizer *)tap{
    UIImageView *tapView = tap.view;
    // SIXPhoto -> MJPhoto
    int i = 0;
    NSMutableArray *arrM = [NSMutableArray array];
    for (SIXPhoto *photo in _photos) {
        
        MJPhoto *p = [[MJPhoto alloc] init];
    
        NSString *urlStr = photo.thumbnail_pic;
        urlStr = [urlStr stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
        p.url = [NSURL URLWithString:urlStr];
        p.index = i;
        p.srcImageView = tapView;
        [arrM addObject:p];
        i++;
    }
    
    // 弹出图片浏览器
    // 创建浏览器对象
    MJPhotoBrowser *brower = [[MJPhotoBrowser alloc] init];
    // MJPhoto
    brower.photos = arrM;
    brower.currentPhotoIndex = tapView.tag;
    SIXLog(@"显示图片");
    [brower show];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 设置图片的尺寸和位置
    NSUInteger photosCount = self.photos.count;
    int maxCol = SIXStatusPhotoMaxCol(photosCount);
    for (int i = 0; i<photosCount; i++) {
        SIXStatusPhotoView *photoView = self.subviews[i];
        
        int col = i % maxCol;
        photoView.x = col * (SIXStatusPhotoWH + SIXStatusPhotoMargin);
        
        int row = i / maxCol;
        photoView.y = row * (SIXStatusPhotoWH + SIXStatusPhotoMargin);
        photoView.width = SIXStatusPhotoWH;
        photoView.height = SIXStatusPhotoWH;
    }
}

+ (CGSize)sizeWithCount:(NSUInteger)count
{
    // 最大列数（一行最多有多少列）
    int maxCols = SIXStatusPhotoMaxCol(count);
    
    //HWStatusPhotosView.m 列数
    NSUInteger cols = (count >= maxCols)? maxCols : count;
    CGFloat photosW = cols * SIXStatusPhotoWH + (cols - 1) * SIXStatusPhotoMargin;
    
    // 行数
    NSUInteger rows = (count + maxCols - 1) / maxCols;
    CGFloat photosH = rows * SIXStatusPhotoWH + (rows - 1) * SIXStatusPhotoMargin;
    
    return CGSizeMake(photosW, photosH);
}
@end
